package com.lxj.cms.manage.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.lxj.core.entity.SYSBaseEntity;

/**
 * Hibernate处理产品分类Tree结构数据
 * 
 * @类功能说明： SessionFactory sessionFactory = new
 *         AnnotationConfiguration().configure().buildSessionFactory(); Session
 *         session = sessionFactory.openSession();
 * 
 *         //一次取出全部节点 Query query = session.createQuery("from MerType as m");
 *         List<MerType> list = query.list(); //从一级缓存中取出根节点（根节点为1000） MerType
 *         root = (MerType) session.get(MerType.class, 1000);
 * 
 *         session.close(); sessionFactory.close();
 * 
 * @类修改者：
 * @修改日期：
 * @修改说明：
 * @公司：昆明天度网络信息科技有限公司
 * @作者：李兴建
 * @创建时间：2013-8-13 上午11:28:57
 * @版本：V1.0
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "tg_product_categories")
public class ProductCategoriesEntity extends SYSBaseEntity {
	
	private String infoTitle;
	/**
	 * 描述
	 */
	private String description;

	private ProductCategoriesEntity parent;

	private List<ProductCategoriesEntity> children;

	public void addChild(ProductCategoriesEntity c) {
		if (children == null) {
			children = new ArrayList<ProductCategoriesEntity>();
		}
		children.add(c);
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getInfoTitle() {
		return infoTitle;
	}

	public void setInfoTitle(String infoTitle) {
		this.infoTitle = infoTitle;
	}

	@ManyToOne(fetch = FetchType.EAGER, targetEntity = ProductCategoriesEntity.class)
	@JoinColumn(name = "P_ID")
	public ProductCategoriesEntity getParent() {
		return parent;
	}

	public void setParent(ProductCategoriesEntity parent) {
		this.parent = parent;
		// 设置父节点的时候，将当前节点加入到父节点的孩子列表中
		if (parent != null) {
			parent.addChild(this);
		}
	}

	@Transient
	public List<ProductCategoriesEntity> getChildren() {
		return children;
	}

	public void setChildren(List<ProductCategoriesEntity> children) {
		this.children = children;
	}
}
